package xlcao.sohutv.ui;

import java.io.IOException;

import xlcao.sohutv.util.FileOperator;
import xlcao.sohutv.util.NanoHTTPD;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;

public class MeleTVServerFragment extends Fragment  {
	
	NanoHTTPD server;
	
	/**
     * Fragment initialization.  We way we want to be retained and
     * start our thread.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Tell the framework to try to keep this fragment around
        // during a configuration change.
        setRetainInstance(true);

        try {
			server = new NanoHTTPD(8999, FileOperator.getInstance().setupWebHome());
			boolean noUpdateDB = PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("noUpdateDB",false);

			if (!noUpdateDB) {
			//	FileOperator.getInstance().updateDB();
			}

			SharedPreferences.Editor mEditor = PreferenceManager.getDefaultSharedPreferences(getActivity()).edit();
			mEditor.putBoolean("noUpdateDB", true);
			mEditor.commit();
    		Log.d("web server", "started");
		} catch (IOException e) {
    		Log.w("web server", "start error");
			e.printStackTrace();
		}
    }
    
    /**
     * This is called when the fragment is going away.  It is NOT called
     * when the fragment is being propagated between activity instances.
     */
    @Override
    public void onDestroy() {
    	if (server != null) {
    		Log.d("web server", "stopped");
    		server.stop();
    	}

        super.onDestroy();
    }
}
